#a=G*(m/d)*unit(d)
#unit(d)=d/module(d)

# PROBLEM 3
#
# Modify the below functions acceleration and 
# ship_trajectory to plot the trajectory of a 
# spacecraft with the given initial position 
# and velocity. Use the Forward Euler Method 
# to accomplish this.

import numpy
import matplotlib.pyplot
import math
# from udacityplots import *

h = 1.0 # s
earth_mass = 5.97e24 # kg
gravitational_constant = 6.67e-11 # N m2 / kg2

def acceleration(spaceship_position):
    
    spaceship_position_module = numpy.linalg.norm(spaceship_position)

    return gravitational_constant*(earth_mass/spaceship_position_module**2)*(-spaceship_position)/spaceship_position_module

def ship_trajectory():
    num_steps = 13000
    x = numpy.zeros([num_steps + 1, 2]) # m
    v = numpy.zeros([num_steps + 1, 2]) # m / s

    x[0, 0] = 15e6
    x[0, 1] = 1e6
    v[0, 0] = 2e3
    v[0, 1] = 4e3

    for i in range(1,num_steps+1):
		x[i] = x[i-1] + h*v[i-1]
		v[i] = v[i-1] + h*acceleration(x[i-1])

    return x, v

x, v = ship_trajectory()

# @show_plot
def plot_me():
    matplotlib.pyplot.plot(x[:, 0], x[:, 1])
    matplotlib.pyplot.scatter(0, 0)
    matplotlib.pyplot.axis('equal')
    axes = matplotlib.pyplot.gca()
    axes.set_xlabel('Longitudinal position in m')
    axes.set_ylabel('Lateral position in m')
    matplotlib.pyplot.show()
plot_me()
    


